import uniPopup from '@/components/uni-popup/uni-popup.vue' // 弹出框组件

export default {
  data() {
    return {
      state: '', //审核状态
      timer: null,
      tipShow: false,
      type: 'new', //认证企业类型 new 注册认证 add 添加企业
      companyName: '', //企业名称
      loginState: 0, //登录信息获取状态 0 未获取 1 获取成功 2 获取失败
      phone: '',
      iknow: true
    }
  },

  onLoad(e) {
    this.state = e.state
    this.phone = e.phone !== 'undefined' && e.phone !== '' ? e.phone : this.$store.state.user.userInfo.mobile
    console.log(this.phone)
    this.type = e.type ? e.type : 'new'

    if (this.state === '0') {
      this.timer = setInterval(() => {
        this.getState()
      }, 3000)
    } else {
      clearInterval(this.timer)
    }
    if (e.type) {
      this.companyName = e.companyName
    } else {

      this.companyName = this.$store.state.userInfoDetail.store ? this.$store.state.userInfoDetail.store
        .companyName : e.companyName

    }
  },

  //监听默认返回事件
  onBackPress(e) {
    if (this.iknow) {
      uni.navigateTo({
        url: '/pages_login/enterCompany/enterCompany?phone=' + this.phone,
      })
      return true
    } else {
      uni.navigateTo({
        url: '/pages_login/register/register?phone=' + this.phone,
      })
      return true
    }

  },

  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
  },

  methods: {
    //重新提交信息 ，重新认证企业
    upInfo() {
      uni.navigateTo({
        url: '/pages_login/register/register?phone=' + this.phone,
      })
    },

    //更新认证状态
    async getState() {
      try {
        const res = await this.$api.user.auditInviteSatus({
          mobile: this.phone,
          companyname: this.companyName
        })
        this.state = res.data
        if (res.data !== 0) {
          if (res.data === 1) {
            this.$refs.popup.open()
          }
          this.iknow = false
          clearInterval(this.timer)
          this.timer = null
          this.quickLogin()
        }
      } catch (e) {
        //TODO handle the exception
      }
    },

    //关闭弹窗
    closePopup() {
      this.$refs.popup.close()
      this.iknow = true
      if (this.type === 'add') {
        uni.navigateBack({
          delta: 2,
        })
      } else {
        if (this.loginState === 1) {
          uni.reLaunch({
            url: '/pages_login/enterCompany/enterCompany?phone=' + this.phone,
          })
        } else if (this.loginState === 2) {
          uni.reLaunch({
            url: '/pages_login/login/login',
          })
        } else {
          uni.showLoading()
          this.timer = setInterval(() => {
            if (this.loginState) {
              uni.hideLoading()
              clearInterval(this.timer)
              this.timer = null
              if (this.loginState === 1) {
                uni.reLaunch({
                  url: '/pages_login/enterCompany/enterCompany?phone=' + this.phone,
                })
              } else if (this.loginState === 2) {
                uni.reLaunch({
                  url: '/pages_login/login/login',
                })
              }
            }
          }, 100)
        }
      }
    },

    async quickLogin() {
      try {
        const res = await this.$api.user.quickLogin({
          Mobile: this.$store.state.user.userInfo.mobile,
          IMEI: this.$store.state.user.imei,
        })
        this.loginSuccess(res.data)
      } catch (err) {
        this.loginState = 2
      }
    },

    //保存登录信息
    loginSuccess(param) {
      this.$store.commit('user/setUserInfo', param)
      this.$store.commit('setUserInfo', param)
      //token存储
      // uni.setStorage({
      //   key: 'token',
      //   data: param.token,
      // })
      // //用户信息存储
      // uni.setStorage({
      //   key: 'Ztc_B_USER_INFO',
      //   data: param,
      // })
      uni.setStorageSync('token', param.token)
      uni.setStorageSync('Ztc_B_USER_INFO', param)
      this.getUserInfo()
    },

    //获取用户信息
    async getUserInfo(cb) {
      try {
        const res = await this.$api.user.getUserInfo()
        uni.setStorageSync('userInfoDetail', res.data)
        this.$store.commit('setUserInfoDetail', res.data)
        this.loginState = 1
      } catch (e) {
        //TODO handle the exception
        this.loginState = 2
      }
    },
  },

  components: {
    uniPopup,
  },
}
